package com.qing.apple.filter.demo02;

import com.qing.apple.filter.bean.Apple;
import com.qing.apple.filter.bean.InitApple;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;


import javax.swing.*;
import java.util.Comparator;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 *  Lambda表达式 真实例子
 */
public class Demo2 {

	public static void main(String[] args) {

		/**
		 * TODO 使用 compareTo 排序
		 * 苹果的重量排序
		 */
		InitApple.getApples().sort(new Comparator<Apple>() {
			@Override
			public int compare(Apple o1, Apple o2) {
				return o1.getWeight().compareTo(o2.getWeight());
			}
		});

		//没有类型推断
		InitApple.getApples().sort((Apple o1,Apple o2)->o1.getWeight().compareTo(o2.getWeight()));
		//类型推断
		InitApple.getApples().sort(( o1, o2)->o1.getWeight().compareTo(o2.getWeight()));



		/**
		 * TODO 使用 Runnable 执行代码块
		 * 苹果的重量排序
		 */
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName()+" hello!");
			}
		},"Thread!").start();


		new Thread(()->{ System.out.println(Thread.currentThread().getName()+" hello!");},"Thread 2").start();

		/**
		 * TODO 通过Callable 返回结果
		 */
		ExecutorService executorService= Executors.newCachedThreadPool();
		Future<String> thread1=executorService.submit(new Callable<String>() {
			@Override
			public String call() throws Exception {
				return Thread.currentThread().getName();
			}
		});

		Future<String> thread2=executorService.submit(()->Thread.currentThread().getName() );

		System.out.println(thread1);
		System.out.println(thread2);

		/**
		 * GUI事件处理
		 */
		JLabel jLabel=new JLabel();
		Button button=new Button("Send");
		button.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				jLabel.setText("Sent!!");
			}
		});

		button.setOnAction((ActionEvent event)->jLabel.setText("Hello!!"));
	}
}
